package com.army.stock.web;

import com.army.stock.domain.ArmyCategory;
import com.army.stock.domain.ArmyGood;
import com.army.stock.service.ArmyCategoryService;
import com.army.stock.service.ArmyGoodService;
import com.army.stock.util.CommonRegExp;
import com.javalife.common.domain.page.PaginatedArrayList;
import com.javalife.common.web.url.BaseUrl;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by zhangyong on 14/12/30.
 * 货品档案
 */
@Controller
public class ArmyGoodController {

    private final static Logger logger = LoggerFactory.getLogger(ArmyGoodController.class);

    @Autowired
    private ArmyCategoryService armyCategoryService;

    @Autowired
    private ArmyGoodService armyGoodService;

    public final static String GOODS = "armygood/goods";

    public final static String NEW_GOOD = "armygood/new";

    public final static String UPDATE_PAGE = "armygood/edit";

    @RequestMapping(value = "/goods", method = RequestMethod.GET)
    public String goods(HttpServletRequest request, ModelMap modelMap) {
        logger.info("进入货品档案列表");
        BaseUrl baseUrl = new BaseUrl(request);
        PaginatedArrayList<ArmyGood> pageInfo = armyGoodService.getArmyGoodPageInfo(
                baseUrl.getCurrentPage(), 10, baseUrl.getQueryMap());
        modelMap.put("page", pageInfo);
        modelMap.put("baseUrl", baseUrl);
        return GOODS;
    }

    @RequestMapping(value = "/goods/new", method = RequestMethod.GET)
    public String addGoodPage(HttpServletRequest request, ModelMap modelMap) {
        logger.info("进入添加货品档案页面");
        List<ArmyCategory> armyCategoryList = armyCategoryService.getAllArmyCategories();
        modelMap.put("armyCategoryList", armyCategoryList);
        return NEW_GOOD;
    }

    @RequestMapping(value = "/goods/add", method = RequestMethod.POST)
    public String add(HttpServletRequest request, ModelMap modelMap, ArmyGood armyGood) {
        logger.info("添加货品档案开始");
        List<ArmyCategory> armyCategoryList = armyCategoryService.getAllArmyCategories();
        modelMap.put("armyCategoryList", armyCategoryList);
        modelMap.put("armyGood", armyGood);
        if (StringUtils.isBlank(armyGood.getGoodCode())) {
            modelMap.put("errorMsg", "物品档案编码不能为空");
            return NEW_GOOD;
        }
        if (!armyGood.getGoodCode().matches(CommonRegExp.CHARACTER_NUMBER)) {
            modelMap.put("errorMsg", "物品档案编码只能为英文中文");
            return NEW_GOOD;
        }
        if (StringUtils.isBlank(armyGood.getGoodName())) {
            modelMap.put("errorMsg", "物品档案名称不能为空");
            return NEW_GOOD;
        }
        if (StringUtils.isBlank(armyGood.getGoodSpec())) {
            modelMap.put("errorMsg", "物品档案规格不能为空");
            return NEW_GOOD;
        }
        if (StringUtils.isBlank(armyGood.getGoodUnit())) {
            modelMap.put("errorMsg", "物品档案单位不能为空");
            return NEW_GOOD;
        }
        if (armyGood.getPrice() == null) {
            modelMap.put("errorMsg", "物品档案价格不能为空");
            return NEW_GOOD;
        }
        //查询有没有重复的货品档案编码
        if (armyGoodService.getArmyGoodByGoodCode(armyGood.getGoodCode()) != null) {
            modelMap.put("errorMsg", "物品档案编码不能重复");
            return NEW_GOOD;
        }
        armyGoodService.addArmyGood(armyGood);
        return "redirect:/goods";
    }

    @RequestMapping(value = "/goods/{goodId}/update", method = RequestMethod.GET)
    public String editPage(@PathVariable(value = "goodId") String goodId, HttpServletRequest request, ModelMap modelMap) {
        logger.info("转向修改商品档案页面supplierId:" + goodId);
        modelMap.put("goodId", goodId);
        ArmyGood armyGood = armyGoodService.getArmyGoodById(Long.valueOf(goodId));
        List<ArmyCategory> armyCategoryList = armyCategoryService.getAllArmyCategories();
        modelMap.put("armyCategoryList", armyCategoryList);
        modelMap.put("armyGood", armyGood);
        return UPDATE_PAGE;
    }

    @RequestMapping(value = "/goods/update", method = RequestMethod.POST)
    public String update(HttpServletRequest request, ModelMap modelMap, ArmyGood armyGood) {
        List<ArmyCategory> armyCategoryList = armyCategoryService.getAllArmyCategories();
        modelMap.put("armyCategoryList", armyCategoryList);
        modelMap.put("armyGood", armyGood);
        if (StringUtils.isBlank(armyGood.getGoodCode())) {
            modelMap.put("errorMsg", "物品档案编码不能为空");
            return UPDATE_PAGE;
        }
        if (!armyGood.getGoodCode().matches(CommonRegExp.CHARACTER_NUMBER)) {
            modelMap.put("errorMsg", "物品档案编码只能为英文中文");
            return UPDATE_PAGE;
        }
        if (StringUtils.isBlank(armyGood.getGoodName())) {
            modelMap.put("errorMsg", "物品档案名称不能为空");
            return UPDATE_PAGE;
        }
        if (StringUtils.isBlank(armyGood.getGoodSpec())) {
            modelMap.put("errorMsg", "物品档案规格不能为空");
            return UPDATE_PAGE;
        }
        if (StringUtils.isBlank(armyGood.getGoodUnit())) {
            modelMap.put("errorMsg", "物品档案单位不能为空");
            return UPDATE_PAGE;
        }
        if (armyGood.getPrice() == null) {
            modelMap.put("errorMsg", "物品档案价格不能为空");
            return UPDATE_PAGE;
        }
        armyGoodService.updateArmyGood(armyGood);
        return "redirect:/goods";
    }

    @RequestMapping(value = "/goods/del", method = RequestMethod.POST)
    @ResponseBody
    public String del(HttpServletRequest request, ModelMap modelMap, ArmyGood armyGood) {
        String id = request.getParameter("id");
        return String.valueOf(armyGoodService.delArmyGood(Long.valueOf(id)));
    }

}
